<?php
/**
 * Note: 渠道LTV统计, 部分数值一直累加
 */

namespace App\Models\Game\Center;

use App\Traits\ModelTrait;

class SourceLtv extends Base
{
    use ModelTrait;

    protected $table = 'source_ltv';

    public $timestamps = false;

    protected $fillable = [
        'day',
        'source',
        'registerUser',
        'loginUser',
        'pay',
        'payUser',
        'withdraw',
        'withdrawUser',
        'thirdExchange',
        'thirdExchangeUser',
        'cplCost',
        'cpaCost',
        'spreadUser',
    ];

    protected $appends = [
        'loginOldUser',
        'registerCost',
        'spreadCost',
        'payCost',
        'roi',
        'roiMarket',
        'platformProfit',
        'normalProfit',
        'operationProfit',
        'payUserScale',
        'withdrawUserScale',
        'thirdExchangePayScale',
        'thirdExchangeUserScale',
    ];

    /**
     * 去新增活跃人数
     * @return float|int
     */
    public function getLoginOldUserAttribute()
    {
        return $this->loginUser - $this->registerUser;
    }

    /**
     * 注册成本 推广费/注册人数
     * @return float|int
     */
    public function getRegisterCostAttribute()
    {
        return $this->registerUser ? $this->spreadCost / $this->registerUser : 0;
    }

    /**
     * @return mixed
     */
    public function getSpreadCostAttribute(): int
    {
        return $this->cplCost + $this->cpaCost;
    }

    /**
     * 付费成本
     * @return float|int
     */
    public function getPayCostAttribute(): float
    {
        return $this->payUser ? $this->spread_cost / $this->payUser : 0;
    }

    /**
     * 投资回报率
     * @return float|int
     */
    public function getRoiAttribute()
    {
        return $this->spreadCost ? (($this->pay - $this->thirdExchange/getCurrencyHbScale(true)) / $this->spreadCost) : 0;
    }

    /**
     * 市场ROI
     */
    public function getRoiMarketAttribute()
    {
        return $this->spreadCost ? $this->pay / $this->spreadCost : 0;
    }

    /**
     * 净利润
     * @return float
     */
    public function getNormalProfitAttribute(): float
    {
        return $this->pay - $this->thirdExchange/getCurrencyHbScale(true) - $this->spreadCost;
    }

    /**
     * 平台毛利
     * @return float
     */
    public function getPlatformProfitAttribute(): float
    {
        return $this->pay - $this->thirdExchange/getCurrencyHbScale(true);
    }

    /**
     * 运营毛利
     * @return float
     */
    public function getOperationProfitAttribute(): float
    {
        return $this->pay - $this->payChannelFee - $this->thirdExchange/getCurrencyHbScale(true) - $this->spreadCost;
    }

    /**
     * 充值率 (充值人数/活跃人数)
     * @return float|int
     */
    public function getPayUserScaleAttribute(): float
    {
        return $this->loginUser ? $this->payUser/$this->loginUser : 0;
    }


    /**
     * 权益率 (权益人数/活跃人数)
     * @return float
     */
    public function getWithdrawUserScaleAttribute(): float
    {
        return $this->loginUser ? $this->withdrawUser/$this->loginUser : 0;
    }

    /**
     * 关联 渠道
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Jenssegers\Mongodb\Relations\BelongsTo
     */
    public function appSource()
    {
        return $this->belongsTo(AppSource::class, 'source', 'id');
    }

    /**
     * 积分占比
     * 积分/充值
     * @return float|int
     */
    public function getThirdExchangePayScaleAttribute()
    {
        return $this->pay ? $this->thirdExchange/getCurrencyHbScale(true)/$this->pay : 0;
    }

    /**
     * 积分率 (积分人数/活跃人数)
     * @return float
     */
    public function getThirdExchangeUserScaleAttribute(): float
    {
        return $this->loginUser ? $this->thirdExchangeUser/$this->loginUser : 0;
    }
}
